package com.kuang.org.controller;

import com.kuang.bascic.utils.Ajax;
import com.kuang.bascic.utils.PageList;
import com.kuang.org.domain.DictionaryDetail;
import com.kuang.org.query.DictionaryDetailQuery;
import com.kuang.org.service.DictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/detail")
public class dictionaryDetailController {
    @Autowired
    private DictionaryDetailService dictionaryDetailService;
    @GetMapping
    @ApiOperation(value = "查询" ) //：@ApiOperation(value = "通过ID查询" )打在方法上，对方法做描述
    public List<DictionaryDetail> selecetAllDepartment(){
        List<DictionaryDetail> departments = dictionaryDetailService.selectAllDepartment();
        return departments;
    }
    @GetMapping("/{id}")
    public DictionaryDetail selecetAllDepartmentById(@ApiParam(name="根据id查询传的id",required = true)@PathVariable("id") Long id){
        //@ApiParam(name="ID属性",required = true) ：打在参数前，对参数做描述  required:true 表示必须
        //@PathVariable("id") 这个注解的意思就是接收地址里面的值 给后面值
        DictionaryDetail department = dictionaryDetailService.selectDepartmentById(id);
        return department;
    }
    @PutMapping
    public Ajax addDicType(@RequestBody DictionaryDetail dictionaryDetail){
        //需要返回ajax的格式 所以建立一个工具类返回的格式 里面有两数据：成功的布尔值，和一条string的消息
        //增加和修改放在一起
        // 使用trycath抱起来抛出异常
        System.out.println(dictionaryDetail);
        try {
            if (dictionaryDetail.getId()==null){
                System.out.println(dictionaryDetail);
                // 没有id 即为新增
                dictionaryDetailService.addDepartment(dictionaryDetail);
                return new Ajax();
            }else {
                // 有id的情况为修改
                dictionaryDetailService.UpdateDepartment(dictionaryDetail);
                return new Ajax();
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Ajax(false,"操作失败");
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<DictionaryDetail> selectPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        //RequestBody 接受前端穿的json数据格式 ，@ResponseBody表示返回给前端json数据格式
        PageList<DictionaryDetail> pageList = dictionaryDetailService.pageList1(dictionaryDetailQuery);
        return pageList;
    }
    @PatchMapping
    public Ajax PatchDelete(@RequestBody List<Long> id ) {
        // RequestBody 批量删除也是接受json格式参数
        try {
            dictionaryDetailService.PatchDelete(id);
            return new Ajax();
        } catch (Exception e) {
            e.printStackTrace();
            return new Ajax(false, "操作失败");
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public Ajax deleteDepartment(@ApiParam(value = "删除id",required = true)@PathVariable() Long id){
        try {
            dictionaryDetailService.deleteDepartmentById(id);
            return new Ajax();
        }catch (Exception e){
            e.printStackTrace();
            return new Ajax(false,"操作失败");
        }

    }

}
